package com.htc.callcare.services;


import com.android.internal.telephony.ITelephony;
import com.htc.callcare.datacontroller.VoiceMailFileController;
import com.htc.callcare.infrastructure.globalVar;
import com.htc.callcare.viewcontroller.Popup_Call;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.media.MediaRecorder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Gravity;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.Toast;

public class CallReceiver extends BroadcastReceiver {
	Context mContext;
	Intent intent;
	static String phoneNumber;
	static int type = -1;
	static boolean popUpCallShow = false;
	static Popup_Call popup_Call;
	public static VoiceMailFileController voiceMailFileController;
	// Flag to allow record voice mail
	/*static boolean voiceMail = false;*/
	static boolean voiceMailisRecording = false;
	static boolean istop = false;
	//Check to make sure catch the phone run only 1 time
	static boolean isringing = false;
	public static boolean isRecording;
	MediaRecorder recorder;
	//Check timer thread run
	static boolean runoneTime = false;
	/*//Time out flag
	static boolean timeOut;
	static boolean offHook;*/
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub

		this.mContext = context;
		voiceMailFileController = new VoiceMailFileController(mContext);
		TelephonyManager telephonyManager = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		String temp = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
		if (temp != null && !temp.equals("")) {
			phoneNumber = temp;
			Toast.makeText(context, phoneNumber, Toast.LENGTH_SHORT).show();
		}

		PhoneCallStateListener customPhoneListener = new PhoneCallStateListener(
				context);
		telephonyManager.listen(customPhoneListener,
				PhoneStateListener.LISTEN_CALL_STATE);
	}

	public class PhoneCallStateListener extends PhoneStateListener {

		Context context;

		public PhoneCallStateListener(Context context) {
			this.context = context;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.telephony.PhoneStateListener#onCallStateChanged(int,
		 * java.lang.String)
		 */
		public void onCallStateChanged(int state, String incomingNumber) {
			if (incomingNumber != null && !incomingNumber.equals("")) {
				phoneNumber = incomingNumber;
			}
			switch (state) {
			case TelephonyManager.CALL_STATE_IDLE:
					
				voiceMailFileController.voiceMailEndRecord(/*voiceMail, */isRecording);
				isringing = false;
				/*voiceMail = false;*/
				voiceMailisRecording = false;
				runoneTime = false;
				VoiceMailFileController.setAllowVoiceMail(false);
				VoiceMailFileController.setTimeOut(false);
				/*timeOut = false;
				offHook  =false;*/
				try {
					if (popUpCallShow) {
						popup_Call.endrecord();
						wm.removeView(popup_Call);
						popup_Call = null;
						popUpCallShow = false;

					}
				} catch (IllegalArgumentException e) {
					Log.e(globalVar.logTab, e.toString());
				}

				break;
			case TelephonyManager.CALL_STATE_RINGING:
				// -- check international call or not.
				Log.e("thuutai", "CALL_STATE_RINGING");
				VoiceMailFileController.setTimeOut(false);
				phoneNumber = incomingNumber;
				ITelephony telephonyService = voiceMailFileController
						.getTelephonyService();
				
				if(!runoneTime){
					runoneTime  = true;
					Log.e("thuutai", "Start timer"+incomingNumber);
					voiceMailFileController.Timer(telephonyService, incomingNumber);
				}
				

				if (!popUpCallShow) {
					showPopUp(1, phoneNumber);
				}

				break;
			case TelephonyManager.CALL_STATE_OFFHOOK:
				Log.e("thuutai", "CALL_STATE_OFFHOOK");
				if (incomingNumber != null && !incomingNumber.equals("")) {
					phoneNumber = incomingNumber;
				}
				if (!popUpCallShow) {
					showPopUp(0, phoneNumber);
				}
				Log.e("thuutai", "Stop timer");
				VoiceMailFileController.setTimeOut(true);
				//It 's OK
				if (!voiceMailisRecording&&VoiceMailFileController.isAllowVoiceMail()) {
					voiceMailisRecording = true;
					Intent plugin1 = new Intent(Intent.ACTION_HEADSET_PLUG);
					plugin1.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
					plugin1.putExtra("state", 0);
					plugin1.putExtra("name", "Headset");
					context.sendOrderedBroadcast(plugin1,
							"android.permission.CALL_PRIVILEGED");
					voiceMailFileController.startrecord(phoneNumber);
					Log.e("thuutai", "catch the phone");
					Log.e("thuutai", "voice mail run run....");
					istop = false;
				}
				break;
			}
		}

	};

	static WindowManager wm;

	public void showPopUp(int type, String phoneNumber) {
		popUpCallShow = true;
		wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
		WindowManager.LayoutParams params = new WindowManager.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT,
				WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
						| WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
				WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
				PixelFormat.TRANSLUCENT);
		// ???
		params.x = 250;
		params.height = LayoutParams.WRAP_CONTENT;
		params.width = LayoutParams.FILL_PARENT;
		params.format = PixelFormat.TRANSLUCENT;
		params.gravity = Gravity.TOP;
		popup_Call = new Popup_Call(mContext);
		popup_Call.setType(type);
		popup_Call.setPhoneNumber(phoneNumber);
		wm.addView(popup_Call, params);
	}

	public void removePopUp() {
		wm.removeView(popup_Call);
	}

}
